package com.docmanage.am.service.impl;

import com.docmanage.am.domain.AmArchive;
import com.docmanage.am.domain.AmCabinet;
import com.docmanage.am.domain.AmFolder;
import com.docmanage.am.mapper.AmArchiveMapper;
import com.docmanage.am.mapper.AmCabinetMapper;
import com.docmanage.am.mapper.AmFolderMapper;
import com.docmanage.am.common.constant.CommonConstants;
import com.docmanage.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*** 档案模块，权限检查服务
 * @des
 * @author Ly
 * @date 2023/7/12
 */
@Service
public class AmBrowseService {

    @Autowired
    private AmArchiveMapper amArchiveMapper;
    @Autowired
    private AmFolderMapper amFolderMapper;
    @Autowired
    private AmCabinetMapper amCabinetMapper;
    @Autowired
    private AmBrowseService amBrowseService;
    @Autowired
    private AmFolderService amFolderService;

    /****
     * 查看当前登录用户，是否有某些权限
     * id:文件夹id或者是文件id
     * type:folder,file,文件夹还是文件
     * operate:view,add,update,delete，查看，新增，更新，删除
     * @return
     */
    public void ifHaveAuth(Long id,String type,String operate){


    }



    /**
     * 查询档案文件
     *
     * @param id 档案文件主键
     * @return 档案文件
     */
    public AmArchive viewFile(Long id)
    {
        //需要验证，档案权限
        amBrowseService.ifHaveAuth(id,"file","view");
        // 组装数据
        AmArchive amArchive = amArchiveMapper.selectAmArchiveById(id);
        AmFolder folder = amFolderMapper.selectAmFolderById(amArchive.getFolderId());
        AmCabinet cabinet = amCabinetMapper.selectAmCabinetById(amArchive.getCabinetId());
        if(folder!=null){
            amArchive.setFolderPath(folder.getNpath()+"/"+folder.getName());
        }
        if(cabinet!=null){
            amArchive.setCabinetPath(cabinet.getNpath()+"/"+cabinet.getName());
        }
        return amArchive;
    }

    public String preview(Long id){
        //需要验证，档案权限
        amBrowseService.ifHaveAuth(id,"file","view");
        // 组装数据
        AmArchive amArchive = amArchiveMapper.selectAmArchiveById(id);
        String url = amArchive.getUrl();
        return url;
    }


    /**
     * 查询文件夹
     *
     * @param id 文件夹主键
     * @return 文件夹
     */
    public AmFolder viewFolder(Long id)
    {
        //需要验证，档案权限
        amBrowseService.ifHaveAuth(id,"folder","view");
        //整理数据
        AmFolder folder = amFolderMapper.selectAmFolderById(id);
        List<AmFolder> folderList = amFolderService.selectAllChildren(folder);
        List<Long> idList = new ArrayList<>();
        //所有孩子的id
        for(AmFolder f:folderList){
            idList.add(f.getId());
        }
        //自己的id
        idList.add(folder.getId());
        Long count = amArchiveMapper.selectFileCount(idList);
        folder.setFileCount(count);
        return folder;
    }

    /****
     * 新建文件夹
     * @param name
     * @param parentId
     */
    public void addFolder(String name,Long parentId){
        //需要验证，档案权限
        amBrowseService.ifHaveAuth(parentId,"folder","add");
        //整理数据
        AmFolder folder = new AmFolder();
        folder.setName(name);
        folder.setCreateBy(SecurityUtils.getUsername());
        folder.setParentId(parentId);
        folder.setCreateTime(new Date());
        amFolderMapper.insertAmFolder(folder);
    }

    /****
     * 重命名文件夹
     * @param name
     * @param id
     */
    public void renameFolder(String name,Long id){
        //需要验证，档案权限
        amBrowseService.ifHaveAuth(id,"folder","update");
        //整理数据
        AmFolder folder = new AmFolder();
        folder.setId(id);
        folder.setName(name);
        folder.setUpdateBy(SecurityUtils.getUsername());
        folder.setUpdateTime(new Date());
        amFolderMapper.updateAmFolder(folder);
    }

    /****
     * 重命名文件
     * @param name
     * @param id
     */
    public void renameFile(String name,Long id){
        //需要验证，档案权限
        amBrowseService.ifHaveAuth(id,"file","update");
        //整理数据
        AmArchive archive = new AmArchive();
        archive.setId(id);
        archive.setTitle(name);
        archive.setUpdateBy(SecurityUtils.getUsername());
        archive.setUpdateTime(new Date());
        amArchiveMapper.updateAmArchive(archive);
    }

    /****
     * 移动文件夹
     * @param id
     * @param targetFolderId
     */
    public void moveFolder(Long id,Long targetFolderId){
        //需要验证，档案权限
        amBrowseService.ifHaveAuth(id,"folder","update");
        //整理数据
        AmFolder folder = new AmFolder();
        folder.setId(id);
        folder.setParentId(targetFolderId);
        folder.setUpdateBy(SecurityUtils.getUsername());
        folder.setUpdateTime(new Date());
        amFolderMapper.updateAmFolder(folder);
    }


    /****
     * 移动文件档案
     * @param id
     * @param targetFolderId
     */
    public void moveFile(Long id,Long targetFolderId){
        //需要验证，档案权限
        amBrowseService.ifHaveAuth(id,"file","view");
        //整理数据
        AmArchive archive = new AmArchive();
        archive.setId(id);
        archive.setFolderId(targetFolderId);
        archive.setUpdateBy(SecurityUtils.getUsername());
        archive.setUpdateTime(new Date());
        amArchiveMapper.updateAmArchive(archive);
    }

    /****
     * 删除文件夹
     * @param id
     */
    public void deleteFolder(Long id){
        //验证是否有孩子
        AmFolder queryFolder = new AmFolder();
        queryFolder.setParentId(id);
        queryFolder.setStatus(CommonConstants.ABLE);
        List<AmFolder> folderList = amFolderService.selectAmFolderList(queryFolder);
        if(folderList!=null&&folderList.size()>0){
            throw new RuntimeException("文件夹不为空，不允许删除");
        }
        AmArchive queryFile = new AmArchive();
        queryFile.setAvailable(CommonConstants.ABLE);
        queryFile.setFolderId(id);
        List<AmArchive> fileList = amArchiveMapper.selectAmArchiveList(queryFile);
        if(fileList!=null&&fileList.size()>0){
            throw new RuntimeException("文件夹不为空，不允许删除");
        }
        //需要验证，档案权限
        amBrowseService.ifHaveAuth(id,"folder","delete");
        //整理数据
        AmFolder folder = new AmFolder();
        folder.setId(id);
        folder.setStatus(CommonConstants.DISABLE);
        folder.setUpdateBy(SecurityUtils.getUsername());
        folder.setUpdateTime(new Date());
        amFolderMapper.updateAmFolder(folder);
    }

    /****
     * 删除文件档案
     * @param id
     */
    public void deleteFile(Long id){
        //需要验证，档案权限
        amBrowseService.ifHaveAuth(id,"file","delete");
        //整理数据
        AmArchive archive = new AmArchive();
        archive.setId(id);
        archive.setAvailable(CommonConstants.DISABLE);
        archive.setUpdateBy(SecurityUtils.getUsername());
        archive.setUpdateTime(new Date());
        amArchiveMapper.updateAmArchive(archive);
    }
}
